[BUG] Ensure autolink reference doesn't error when repo is renamed#3271
[BUG] Ensure autolink reference doesn't error when repo is renamed#3271deiga wants to merge 10 commits into
Conversation
|
👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with |
ad1fdb7 to
00ea1c8
Compare
00ea1c8 to
458f672
Compare
stevehipwell
left a comment
There was a problem hiding this comment.
Do we need a migration to add the repository ID and then rely on it or we just supporting it as a best effort?
458f672 to
852d646
Compare
852d646 to
50a9aa7
Compare
There was a problem hiding this comment.
Pull request overview
These provider review instructions are being used.
This PR fixes #3470 for github_repository_autolink_reference so that renaming a repository no longer forces the autolink reference to be destroyed and recreated (which previously errored). It adopts the provider's rename-safe convention: drop ForceNew from repository, add a computed repository_id, and wire up CustomizeDiff: diffRepository so replacement is only forced when the underlying repository ID actually changes. The CRUD handlers are also modernized to the *Context variants, and a state upgrader is added to backfill repository_id.
Changes:
- Convert CRUD to context-aware handlers, add a no-op
Update, extract the importer, and switch logging totflog. - Add
repository_id(computed), removeForceNewfromrepository, and addCustomizeDiff: diffRepository; add a state upgrader + migration to populaterepository_id. - Update acceptance tests to
statecheck/plancheck(incl. a rename scenario), add a migration unit test, and refresh docs/example.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| github/resource_github_repository_autolink_reference.go | Rename-safe schema (repository_id, no ForceNew, diffRepository), context CRUD, no-op Update; state upgrader registered with a version mismatch (blocking). |
| github/resource_github_repository_autolink_reference_migration.go | New V0 schema + state upgrade func that fetches and stores repository_id. |
| github/resource_github_repository_autolink_reference_migration_test.go | Unit test invoking the state upgrade function directly against a mocked API. |
| github/resource_github_repository_autolink_reference_test.go | Migrates checks to statecheck, adds a repository-rename in-place test. |
| templates/resources/repository_autolink_reference.md.tmpl | Docs template: updated repository description, adds repository_id. |
| docs/resources/repository_autolink_reference.md | Generated docs mirror the template/schema updates and visibility example. |
| examples/resources/repository_autolink_reference/example_1.tf | Example switched from deprecated private to visibility. |
Blocking finding: the added StateUpgrader uses Version: 0 while the resource already ships at SchemaVersion: 1, so it will never execute for existing state (which is already stamped v1). The version must be bumped to 2 with the upgrader registered at Version: 1 to actually backfill repository_id.
cf7ee3b to
4b95c1a
Compare
stevehipwell
left a comment
There was a problem hiding this comment.
Just a quick review.
| | `github_release` | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | | ||
| | `github_repository` | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | | ||
| | `github_repository_autolink_reference` | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | | ||
| | `github_repository_autolink_reference` | ⚠️ | ✅ | ✅ | ✅ | ✅ | ❓ | ❓ | |
There was a problem hiding this comment.
| | `github_repository_autolink_reference` | ⚠️ | ✅ | ✅ | ✅ | ✅ | ❓ | ❓ | | |
| | `github_repository_autolink_reference` | ⚠️ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ | |
| func resourceGithubRepositoryAutolinkReferenceDelete(d *schema.ResourceData, meta any) error { | ||
| client := meta.(*Owner).v3client | ||
| func resourceGithubRepositoryAutolinkReferenceUpdate(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| tflog.Warn(ctx, "Update function of autolink reference. This should not be called. But it's necessary when 'repository' doesn't have `ForceNew`", map[string]any{ |
There was a problem hiding this comment.
Could we make this a single line?
| parts := strings.Split(d.Id(), "/") | ||
| if len(parts) != 2 { | ||
| return nil, fmt.Errorf("invalid ID specified: supplied ID must be written as <repository>/<autolink_reference_id>") | ||
| } |
There was a problem hiding this comment.
I think the ID should be migrated as part of the schema migration so it can use the ID functions.
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
Signed-off-by: Timo Sand <timo.sand@f-secure.com>
4b95c1a to
aaa15da
Compare
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| return resourceGithubRepositoryAutolinkReferenceRead(ctx, d, m) |
There was a problem hiding this comment.
We should be able to remove this call and just set the etag.
| func resourceGithubRepositoryAutolinkReferenceRead(d *schema.ResourceData, meta any) error { | ||
| client := meta.(*Owner).v3client | ||
| func resourceGithubRepositoryAutolinkReferenceRead(ctx context.Context, d *schema.ResourceData, m any) diag.Diagnostics { | ||
| ctx = tflog.SetField(ctx, "id", d.Id()) |
There was a problem hiding this comment.
Let's keep with the current pattern where we only set attributes when we need them. And as a rule of thumb id shouldn't be set unless there is an unexpected error, we want the field named in a friendly way (e.g. autolink_id).
| autolinkRefID, err := strconv.ParseInt(d.Id(), 10, 64) | ||
| if err != nil { | ||
| return unconvertibleIdErr(d.Id(), err) | ||
| return diag.FromErr(unconvertibleIdErr(d.Id(), err)) | ||
| } |
There was a problem hiding this comment.
Why not add a new idStringToInt64OK function with the signature func(string) (bool, diag.Diagnostics) (similar to the checkOrganizationOK I'm adding in #3537)?
| if !d.IsNewResource() { | ||
| ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) | ||
| } |
There was a problem hiding this comment.
This can be removed as create shouldn't be calling through.
| if ghErr.Response.StatusCode == http.StatusNotFound { | ||
| log.Printf("[INFO] Removing autolink reference for repository %s/%s from state because it no longer exists in GitHub", | ||
| owner, repoName) | ||
| tflog.Info(ctx, "Autolink reference not found, removing from state.", map[string]any{ |
There was a problem hiding this comment.
Please make this a single line.
| return nil, err | ||
| } | ||
|
|
||
| return []*schema.ResourceData{d}, nil |
There was a problem hiding this comment.
You need to set all of the fields as they're force new.
| return nil, fmt.Errorf("state upgrade v1: repository is not a string or not set") | ||
| } | ||
|
|
||
| repo, _, err := client.Repositories.Get(ctx, owner, repoName) |
There was a problem hiding this comment.
You can use the migrateRepositoryWithID function to avoid repeating the code.
| "github.com/google/go-github/v89/github" | ||
| ) | ||
|
|
||
| func buildMockResponsesForRepositoryAutolinkReferenceMigrationV1toV2(mockOwner, mockRepo string, wantRepoID int) []*mockResponse { |
There was a problem hiding this comment.
You shouldn't need this, please copy the pattern used for Test_resourceGithubCustomPropertyStateUpgradeV0.
| randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum) | ||
| repoName := fmt.Sprintf("%srepo-autolink-%s", testResourcePrefix, randomID) | ||
| config := fmt.Sprintf(` | ||
| resource "github_repository" "test" { |
There was a problem hiding this comment.
Please switch to using the test helpers to create the repo instead of using TF.
| { | ||
| Config: config, | ||
| Check: check, | ||
| ConfigStateChecks: []statecheck.StateCheck{ |
There was a problem hiding this comment.
I'm pretty sure you don't need the checks given that none are computed (and we should ignore etag).
Addresses #3470 for
github_repository_autolink_referenceBefore the change?
github_repository_autolink_referenceto be re-created and throw an errorAfter the change?
github_repository_autolink_referencePull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!